package com.micookie.v2ex.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.micookie.v2ex.R;
import com.micookie.v2ex.model.bean.Node;
import com.micookie.v2ex.ui.NodeTopicListActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by Administrator on 2016-10-25.
 */

public class NodeListAdapter extends RecyclerView.Adapter<NodeListAdapter.NodeViewHolder> {

    private Context mContext;

    public List<Node> getDataList() {
        return mDataList;
    }

    private List<Node> mDataList;

    public NodeListAdapter(Context context, List<Node> dataList) {
        this.mContext = context;
        this.mDataList = dataList;
    }

    @Override
    public NodeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new NodeViewHolder(LinearLayout.inflate(mContext, R.layout.item_node, null));
    }

    @Override
    public void onBindViewHolder(NodeViewHolder holder, int position) {
        Node node = mDataList.get(position);
        holder.setData(node);
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }


    /**
     * ***********************************
     * **********ViewHolder***************
     * ***********************************
     */

    class NodeViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.node_layout_ll)
        LinearLayout mNodeLayoutLl;

        @BindView(R.id.node_title_tv)
        TextView mTitleTv;

        @BindView(R.id.node_header_tv)
        TextView mHeaderTv;

        @BindView(R.id.node_count_tv)
        TextView mCountTv;

        public NodeViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void setData(final Node node) {
            mTitleTv.setText(node.getTitle());
            mHeaderTv.setText(Html.fromHtml(node.getHeader()));
            mCountTv.setText(node.getTopicCountString() + "个主题");

            mNodeLayoutLl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    NodeTopicListActivity.startActivity(mContext, node);
                }
            });
        }
    }

}
